package q1022_sumRootToLeaf;
import CommonClass.Common.TreeNode;
public class Solution_2 {
    public int sumRootToLeaf(TreeNode root) {
        return dfs(root, 0);
    }

    private int dfs(TreeNode root, int val) {
        if (root == null) return 0;
        val = val << 1 | root.val;
        // 遇到叶子节点就返回
        if (root.left == null && root.right == null) return val;
        // 否则就后序遍历 返回左右两个树的和
        return dfs(root.left, val) + dfs(root.right, val);
    }

}
